package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BmsSightseeingBusApplicantParam;
import com.macro.mall.mapper.BmsSightseeingBusApplicantMapper;
import com.macro.mall.model.BmsSightseeingBusApplicant;
import com.macro.mall.service.BmsSightseeingBusApplicantService;
import com.macro.mall.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.List;



/**
 * 观光车点位Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsSightseeingBusApplicantServiceImpl implements BmsSightseeingBusApplicantService {
    @Autowired
    private BmsSightseeingBusApplicantMapper bmsSightseeingBusApplicantMapper;
    @Autowired
    private UserUtils userUtils;


    /**
     * 分页查询观光车
     * @return
     */
    @Transactional
    @Override
    public List<BmsSightseeingBusApplicant> getList(BmsSightseeingBusApplicantParam bmsSightseeingBusApplicantParam) {
        PageHelper.startPage(bmsSightseeingBusApplicantParam.getPageNum(), bmsSightseeingBusApplicantParam.getPageSize());
        BmsSightseeingBusApplicant bmsSightseeingBusApplicant = new BmsSightseeingBusApplicant();
        BeanUtils.copyProperties(bmsSightseeingBusApplicantParam, bmsSightseeingBusApplicant);
        List<BmsSightseeingBusApplicant> list = bmsSightseeingBusApplicantMapper.selectAll(bmsSightseeingBusApplicant);
        return list;
    }

    /**
     * 新增观光车
     *
     * @param
     * @return
     */
    @Transactional
    @Override
    public BmsSightseeingBusApplicant create(BmsSightseeingBusApplicantParam bmsSightseeingBusApplicantParam) throws IOException, WriterException {
        BmsSightseeingBusApplicant bmsSightseeingBusApplicant = new BmsSightseeingBusApplicant();
        try{
            BeanUtils.copyProperties(bmsSightseeingBusApplicantParam, bmsSightseeingBusApplicant);
            bmsSightseeingBusApplicant.setCreateUser(bmsSightseeingBusApplicantParam.getUserId());
            bmsSightseeingBusApplicant.setUpdateUser(bmsSightseeingBusApplicantParam.getUserId());
            bmsSightseeingBusApplicant.setCreateTime(new Date());
            bmsSightseeingBusApplicant.setUpdateTime(new Date());
            bmsSightseeingBusApplicant.setApplicationTime(new Date());
            //生成二维码并保存
            String qrCode = generateQRCodeImage(String.valueOf(bmsSightseeingBusApplicant.getUserId()));
            bmsSightseeingBusApplicant.setQrCode(qrCode);

            if (bmsSightseeingBusApplicantMapper.insert(bmsSightseeingBusApplicant) > 0){
                bmsSightseeingBusApplicant = bmsSightseeingBusApplicantMapper.selectSuccess(bmsSightseeingBusApplicant.getId());
            }else{
                return new BmsSightseeingBusApplicant();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bmsSightseeingBusApplicant;
    }

    //生成二维码
    public static String generateQRCodeImage(String text) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 200, 200);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        String base64Image = Base64.getEncoder().encodeToString(pngData);
        pngOutputStream.close();
        return base64Image;
    }






    /**
     * 取消预约
     * @param
     * @return
     */
    @Transactional
    @Override
    public int update(BmsSightseeingBusApplicantParam bmsSightseeingBusApplicantParam) {
        BmsSightseeingBusApplicant bmsSightseeingBusApplicant = new BmsSightseeingBusApplicant();
        BeanUtils.copyProperties(bmsSightseeingBusApplicantParam, bmsSightseeingBusApplicant);
        return bmsSightseeingBusApplicantMapper.update(bmsSightseeingBusApplicant);
    }


    /**
     * 获取单个观光车点位信息
     * @param id
     * @return
     */
    @Override
    public BmsSightseeingBusApplicant getItem(Long id) {
        BmsSightseeingBusApplicant list = bmsSightseeingBusApplicantMapper.selectDetail(id);
        return list;

    }

    /**
     * 删除观光车点位
     * @param ids
     * @return
     * 完成
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        return bmsSightseeingBusApplicantMapper.delete(ids);

    }


}
